﻿using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;
using common;

namespace tools{
	public class cashDataLoader {
		
		// Use this for initialization
		string path = "";
		public static string coder = "$";

		public string LoadNotice(){
			return LoadFile ("user.txt",false);
		}

		public void RemoveUser(){
			FileInfo t;

			if (Application.platform == RuntimePlatform.IPhonePlayer) {
				t = new FileInfo (Application.persistentDataPath + "//" + "user.txt");
			} else if (Application.platform == RuntimePlatform.Android) {
				t = new FileInfo (Application.persistentDataPath + "/" + "user.txt");
			} else {
				t = new FileInfo (Application.streamingAssetsPath + "/" + "user.txt");
			}
			//文件流信息
			StreamWriter sw;

			if(t.Exists) {
				t.Delete();
			}
		}

		public void RemoveVoice(string voiveName){
			FileInfo t;

			if (Application.platform == RuntimePlatform.IPhonePlayer) {
				t = new FileInfo (Application.persistentDataPath + "//" + voiveName+".amr");
			} else if (Application.platform == RuntimePlatform.Android) {
				t = new FileInfo (Application.persistentDataPath + "/" + voiveName+".amr");
			} else {
				t = new FileInfo (Application.streamingAssetsPath + "/" + voiveName+".amr");
			}
			//文件流信息
			StreamWriter sw;

			if(t.Exists) {
				Debug.Log(voiveName+".amr 存在");
				t.Delete();
				Debug.Log(voiveName+".amr 已经删除");
			}
		}

		public string getSoundVoice(){
			return LoadFile ("sound.txt");
		}

		public void setSoundVoice(string value){
			CreateFile("sound.txt",value);
		}
		
		public void CreateFile(string name,string info) {
			Debug.Log ("1");
	
			FileInfo t;
			
			if (Application.platform == RuntimePlatform.IPhonePlayer) {
				t = new FileInfo (Application.persistentDataPath + "//" + name);
			} else if (Application.platform == RuntimePlatform.Android) {
				t = new FileInfo (Application.persistentDataPath + "/" + name);
			} else {
				t = new FileInfo (Application.streamingAssetsPath + "/" + name);
			}
			Debug.Log ("2");
			//文件流信息
			StreamWriter sw;

			if(!t.Exists) {
				t.Delete();
				Debug.Log ("3");
			} 

			sw = t.CreateText ();
			string newInfo = "";
			Debug.Log ("5");
			newInfo = Common.CodeString (info);
			sw.WriteLine(newInfo);
			sw.Close();
			sw.Dispose();
			Debug.Log ("6");
			sw = null;
		}  
		
		public string startLoad () {
			string txt = LoadFile ("cash.txt");
			return txt;
		}
		
		private string LoadFile(string name,bool needDecode=true)
		{
			StreamWriter sw;
			FileInfo t;
			Debug.Log ("11");
			if (Application.platform == RuntimePlatform.IPhonePlayer) {
				t = new FileInfo(Application.persistentDataPath+"//"+ name);
			} else if (Application.platform == RuntimePlatform.Android) {
				t = new FileInfo (Application.persistentDataPath+"/"+ name);
			} else {
				t = new FileInfo (Application.streamingAssetsPath+"/"+ name);
			}
			
			if (!t.Exists) {
				Debug.Log ("12");
				sw = null;
				return null;
			}

			Debug.Log ("13");
			//使用流的形式读取
			StreamReader sr = null;
			
			try {
				if (Application.platform == RuntimePlatform.Android) {
					sr = File.OpenText (Application.persistentDataPath + "/" + name);
				} else {
					sr = t.OpenText();
					//sr = File.OpenText (Application.streamingAssetsPath+"/"+ name);
				}
			} catch (IOException e) {
				//路径与名称未找到文件则直接返回空
				return null;
			}

			Debug.Log ("14");
			string line;
			line = sr.ReadToEnd ();
			line = Common.DeCodeString (line);
			Debug.Log ("15:"+line);

			sr.Close ();
			sr.Dispose ();
			sr = null;
			return line;
		}
	}
}